<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.07.23.
   家居公司商家刷新模型($$)*/

!defined('Q_PATH') && exit;

class JiajucompanyrefreshModel extends CommonModel{

	static public function init__(){
		return array(
			'table_name'=>'jiajucompanyrefresh',
		);
	}

	static function F(){
		$arrArgs=func_get_args();
		return ModelMeta::instance(__CLASS__)->findByArgs($arrArgs);
	}

	static function M(){
		return ModelMeta::instance(__CLASS__);
	}

	/**
	 * 刷新指定的商铺
	 */
	public function refreshCompany($nCompanyId){
		$nStartTime=mktime(0,0,0,date('m'),date('d'),date('Y'));
		$nEndTime=mktime(0,0,0,date('m'),date('d')+1,date('Y'));
		
		// 检测今天已经刷新的次数
		$nCount=self::F()->where(array('jiajucompany_id'=>$nCompanyId,'refresh_time'=>array('between',array($nStartTime,$nEndTime))))->getCounts();
		
		if($GLOBALS['_company_']['jiajucompany_isfvip']==1 && $nCount>=$GLOBALS['_commonConfig_']['SHOP_LIMIT'][1]['REFRESH_TIMES']){
			$this->_sErrorMessage='你的VIP商铺每天可以刷新'.$GLOBALS['_commonConfig_']['SHOP_LIMIT'][1]['REFRESH_TIMES'].'次，今天的刷新次数已经用完';
			return false;
		}
		
		if($GLOBALS['_company_']['jiajucompany_isfvip']==0 && $nCount>=$GLOBALS['_commonConfig_']['SHOP_LIMIT'][0]['REFRESH_TIMES']){
			$this->_sErrorMessage='普通商铺每天可以刷新'.$GLOBALS['_commonConfig_']['SHOP_LIMIT'][0]['REFRESH_TIMES'].'次，今天的刷新次数已经用完';
			return false;
		}
		
		// 检测刷新间隔时间（获取最后一条记录时间）
		$nLastCompanyrefresh=self::F()->order('refresh_time DESC')->where(array('jiajucompany_id'=>$nCompanyId))->getColumn('refresh_time');
		if($nLastCompanyrefresh && CURRENT_TIMESTAMP-$nLastCompanyrefresh<$GLOBALS['_commonConfig_']['REFRESH_LIMITTIME']){
			$this->_sErrorMessage='您刷的太猛了，刷新间隔时间未到，最短刷新间隔时间为'.$GLOBALS['_commonConfig_']['REFRESH_LIMITTIME'].'秒';
			return false;
		}
		
		// 写入刷新记录数据
		Model::M_('jiajucompanyrefresh')->insertWhere(
			array(
				'jiajucompany_id'=>$nCompanyId,
				'refresh_time'=>CURRENT_TIMESTAMP,
			)
		);

		// 更新商铺刷新时间
		Model::M_('jiajucompany')->updateWhere(
			array(
				'jiajucompany_refreshdateline'=>CURRENT_TIMESTAMP,
			),'jiajucompany_id=?',$nCompanyId
		);

		// 清理昨天的刷新记录数据
		Model::M_('jiajucompanyrefresh')->deleteWhere(
			array(
				'jiajucompany_id'=>$nCompanyId,
				'refresh_time'=>array('lt'=>$nStartTime),
			)
		);

		return true;
	}

}
